//
//  SLInformationTableViewCell.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/22.
//

#import "SLInformationTableViewCell.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLInformationOptionComponent.h"
#import "OriginData.h"
@interface SLInformationTableViewCell ()
@property (nonatomic, strong) UIView *bgView;// 容器
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UIStackView *stackView;// 容器
@end
@implementation SLInformationTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColorHex(#F5F5F5);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.decorate];
        
        [self.bgView addSubview:self.stackView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(0);
            make.top.equalTo(self.contentView).offset(25*0.5);
            make.bottom.equalTo(self.contentView);
        }];
        [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.centerY.equalTo(self.bgView.mas_top);
            make.height.mas_equalTo(25);
        }];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(34);
            make.left.right.equalTo(self.bgView).inset(35);
            make.bottom.equalTo(self.bgView).inset(155);
        }];
    }
    return self;
}

- (void)setItems:(NSArray *)items {
    _items = items;
    [self.stackView removeAllSubviews];
    
    for (NSDictionary *dict in items) {
        SLInformationOptionComponent *item = [self optionItem];
        // 赋值
        NSString *cheerfully = dict[@"cheerfully"]; //表单类型
        NSString *formType = [OriginData cheerfullyOrigin:cheerfully];
        item.titleLabel.text = dict[@"conception"] ?: @"Name";
        item.textField.placeholder = dict[@"thoroughly"];
        item.textField.text = dict[@"lake"];
        item.textField.keyboardType = [dict[@"mast"] sl_intValue] == 1 ? UIKeyboardTypeNumberPad : UIKeyboardTypeDefault;
        
        if (![formType isEqualToString:@"txt"]) {
            @weakify(self);
            item.textField.enabled = NO;
            [item jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                @strongify(self);
                !self.itemClickCallback ?: self.itemClickCallback([items indexOfObject:dict]);
            }];
        } else {
            [item.textField addBlockForControlEvents:UIControlEventEditingChanged block:^(UITextField *sender) {
                [dict setValue:sender.text forKey:@"lake"];
                [dict setValue:sender.text forKey:@"peppers"];
            }];
        }
        item.more.hidden = [formType isEqualToString:@"txt"];
        
        [self.stackView addArrangedSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(65);
        }];
    }
}

- (SLInformationOptionComponent *)optionItem {
    SLInformationOptionComponent *item = [SLInformationOptionComponent new];
    return item;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.cornerRadius = 20;
        _bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _bgView.layer.borderWidth = 1.5;
    }
    return _bgView;
}
- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
        _decorate.image = [UIImage imageNamed:@"icon_circle_bar"];
    }
    return _decorate;
}
- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.spacing = 19;
        _stackView.axis = UILayoutConstraintAxisVertical;
    }
    return _stackView;
}
@end
